package com.lungle.warmdiary.app.content.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.content.db.helper.CacheSQLiteOpenHelper;
import com.lungle.warmdiary.app.utils.LogUtils;

/**
 * Created by zhangxueyuan on 16/10/6.
 * <p>
 * 评测填写
 */
public class CommentDAO extends BaseDAO {
    private CacheSQLiteOpenHelper helper;
    private String DB_NAME = AppConstant.CACHE_DB;
    private String TABLE = "T_campaign_comment";


    public CommentDAO(Context context) {
        this.helper = new CacheSQLiteOpenHelper(context, DB_NAME);
    }

    public boolean insert(String pid, int rate, String content, String images) {
        db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("pid", pid);
        values.put("rate", rate);
        values.put("content", content);
//        if (images != null)
        values.put("images", images);

        long result = db.insert(TABLE, null, values);
        close();
        return result == -1 ? false : true;
    }


    public boolean update(String pid, int rate, String content, String images) {
        db = helper.getWritableDatabase();
        String where = String.format("pid = %s", pid);
        ContentValues values = new ContentValues();

        values.put("rate", rate);
        values.put("content", content);
//        if (images != null)
        values.put("images", images);

        long result = db.update(TABLE, values, where, null);
        close();
        return result == -1 ? false : true;
    }


    public void remove(String pid) {
        // TODO: 16/10/6 remove and update
        db = helper.getWritableDatabase();
        String where = String.format("pid = %s", pid);
        db.delete(TABLE, where, null);
        close();

    }

    public void removeAll() {
        db = helper.getWritableDatabase();
        db.delete(TABLE, null, null);
        db.endTransaction();
    }


    public Comment find(String pid) {
        db = helper.getReadableDatabase();
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("select * from " + TABLE + " where pid=? ", new String[]{String.valueOf(pid)});
            while (cursor.moveToNext()) {
                Comment comment = new Comment();
                comment.content = cursor.getString(cursor.getColumnIndex("content"));
                comment.rate = cursor.getInt(cursor.getColumnIndex("rate"));
                comment.images = cursor.getString(cursor.getColumnIndex("images"));
                return comment;
            }
        } catch (Exception e) {
            LogUtils.printLogW(e.getMessage());
        } finally {
            if (cursor != null) cursor.close();
        }

        return null;
    }

    public class Comment {
        int rate;
        String content;
        String images;

        public int getRate() {
            return rate;
        }

        public String getContent() {
            return content;
        }

        public String getImages() {
            return images;
        }

        @Override
        public String toString() {
            return "Comment{" +
                    "rate=" + rate +
                    ", content='" + content + '\'' +
                    ", images='" + images + '\'' +
                    '}';
        }
    }

}
